package com.jackiechancinema.ssm.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jackiechancinema.ssm.bo.SalaryBo;
import com.jackiechancinema.ssm.config.SystemContant;
import com.jackiechancinema.ssm.dao.SalManageDao;
import com.jackiechancinema.ssm.dto.SalaryDetailDto;
import com.jackiechancinema.ssm.dto.SalaryDto;
import com.jackiechancinema.ssm.dto.SalaryExcelDto;
import com.jackiechancinema.ssm.entity.Salary;
import com.jackiechancinema.ssm.entity.Work;
import com.jackiechancinema.ssm.listener.SalaryReadListener;
import com.jackiechancinema.ssm.service.Inte.SalaryService;
import com.jackiechancinema.ssm.utils.BeanUtil;
import com.jackiechancinema.ssm.vo.PageVo;
import com.jackiechancinema.ssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import javax.rmi.CORBA.Util;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@Service
public class SalaryServiceImpl implements SalaryService {
    @Autowired
    SalManageDao dao;

    @Autowired
    SalaryReadListener listener;

    @Override
    public PageVo<SalaryDto> queryPage(SalaryBo bo) {
        if (bo != null) {
            SalaryDto salaryDto = new SalaryDto();
            Work work = new Work();
            salaryDto.setWork(work);
            if ((bo.getEno() != null) && (bo.getEno()) > -1) {
                salaryDto.setEmpno(bo.getEno());
            }
            if ((bo.getMonth() != null) && (bo.getMonth()) > -1) {
                salaryDto.setMonth(bo.getMonth());
            }
            if ((bo.getName()) != null) {
                if (salaryDto.getWork() != null) {
                    salaryDto.getWork().setWork_name(bo.getName());
                }
            }
            PageHelper.startPage(bo.getPage(), bo.getLimit());
            PageInfo<SalaryDto> pageInfo = new PageInfo<SalaryDto>(dao.selectBySerch(salaryDto));
            return new PageVo<SalaryDto>(SystemContant.PAGE_R, "OK", pageInfo.getTotal(), pageInfo.getList());
        }
        return new PageVo<SalaryDto>(SystemContant.PAGE_R, "OK", 0, null);
    }

    @Override
    public R delete(int id) {
        if (dao.deleteById(id) > 0) {
            return R.ok("Ok");
        }
        return R.fail();
    }


    @Override
    public R updateSalary(SalaryDto salaryDto) {
        if (dao.update(salaryDto) > 0) {
            return R.ok("OK");
        } else {
            return R.fail();
        }
    }

    @Override
    public R save(Salary salary) {
        if (dao.insert(salary) > 0) {
            return R.ok("OK");
        } else {
            return R.fail();
        }
    }

    @Override
    public R batchAdd(MultipartFile file) {
        if (!file.isEmpty()) {
            //excel解析
            try {
                EasyExcel.read(file.getInputStream(), SalaryExcelDto.class, listener).sheet().doRead();
                return R.ok("OK");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.fail();
    }

    @Override
    public void exportExcel(SalaryBo bo, HttpServletResponse response) {
        String fn = System.currentTimeMillis() + ".xlsx";
        response.setHeader("content-disposition", "attachment;filename=" + fn);

        if (bo != null) {
            SalaryDto salaryDto = new SalaryDto();
            Work work = new Work();
            salaryDto.setWork(work);
            if ((bo.getEno() != null) && (bo.getEno()) > -1) {
                salaryDto.setEmpno(bo.getEno());
            }
            if ((bo.getMonth() != null) && (bo.getMonth()) > -1) {
                salaryDto.setMonth(bo.getMonth());
            }
            if ((bo.getName()) != null) {
                if (salaryDto.getWork() != null) {
                    salaryDto.getWork().setWork_name(bo.getName());
                }
            }
            //3.获取要下载的数据
            List<SalaryDto> list = dao.selectBySerch(salaryDto);
            List<SalaryDetailDto> sdtList = new ArrayList<SalaryDetailDto>();
            for (SalaryDto dtos:list) {
                SalaryDetailDto salaryDetailDto = BeanUtil.copyProperty(SalaryDetailDto.class, dtos, dtos.getClass().getFields());
                salaryDetailDto.setId(dtos.getId());
                salaryDetailDto.setEmpno(dtos.getEmpno());
                salaryDetailDto.setMonth(dtos.getMonth());
                salaryDetailDto.setBasicsalary(dtos.getBasicsalary());
                salaryDetailDto.setReward(dtos.getReward());
                salaryDetailDto.setTaxes(dtos.getTaxes());
                salaryDetailDto.setInsurance(dtos.getInsurance());
                salaryDetailDto.setCtime(dtos.getCtime());
                sdtList.add(salaryDetailDto);
            }

            try {
                //4.生成Excel，并将数据写出到响应流中
                EasyExcel.write(response.getOutputStream(), SalaryDetailDto.class).sheet().doWrite(sdtList);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
